home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite.X11R3 / RCS / buf.c,v < prev    next >
Encoding:
Text File  |  1989-10-26  |  9.9 KB  |  445 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     88.09.08.18.16.18;  author ouster;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.09.02.17.52.28;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Intermediate check-in while converting to new C library.
  27. @
  28. text
  29. @/*-
  30.  * buf.c --
  31.  *    Functions for automatically-expanded buffers.
  32.  *
  33.  * Copyright (c) 1987 by the Regents of the University of California
  34.  *
  35.  * Permission to use, copy, modify, and distribute this
  36.  * software and its documentation for any purpose and without
  37.  * fee is hereby granted, provided that the above copyright
  38.  * notice appear in all copies.  The University of California
  39.  * makes no representations about the suitability of this
  40.  * software for any purpose.  It is provided "as is" without
  41.  * express or implied warranty.
  42.  *
  43.  *
  44.  */
  45. #ifndef lint
  46. static char rcsid[] =
  47. "$Header: buf.c,v 1.1 88/09/02 17:52:28 ouster Exp $ SPRITE (Berkeley)";
  48. #endif lint
  49.  
  50. #include <bstring.h>
  51. #include <stdlib.h>
  52. #include    "buf.h"
  53.  
  54. typedef struct {
  55.     int        size;     /* Current size of the buffer */
  56.     Byte    *buffer;    /* The buffer itself */
  57.     Byte    *inPtr;    /* Place to write to */
  58.     Byte    *outPtr;    /* Place to read from */
  59. } Buf, *BufPtr;
  60.  
  61. #ifndef max
  62. #define max(a,b)  ((a) > (b) ? (a) : (b))
  63. #endif
  64.  
  65. /*
  66.  * BufExpand --
  67.  *     Expand the given buffer to hold the given number of additional
  68.  *    bytes.
  69.  */
  70. #define BufExpand(bp,nb) \
  71.      if (((bp)->size - ((bp)->inPtr - (bp)->buffer)) < (nb)) {\
  72.         int newSize = (bp)->size + max((nb),BUF_ADD_INC); \
  73.         Byte  *newBuf = (Byte *) malloc ((unsigned) newSize); \
  74.         \
  75.         bcopy ((bp)->outPtr, newBuf, (bp)->inPtr - (bp)->outPtr); \
  76.         (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->outPtr); \
  77.         (bp)->outPtr = newBuf;\
  78.         free ((char *) (bp)->buffer);\
  79.         (bp)->buffer = newBuf;\
  80.         (bp)->size = newSize;\
  81.     }
  82.  
  83. #define BUF_DEF_SIZE    256     /* Default buffer size */
  84. #define BUF_ADD_INC    256     /* Expansion increment when Adding */
  85. #define BUF_UNGET_INC    16      /* Expansion increment when Ungetting */
  86.  
  87. /*-
  88.  *-----------------------------------------------------------------------
  89.  * Buf_AddByte --
  90.  *    Add a single byte to the buffer.
  91.  *
  92.  * Results:
  93.  *    None.
  94.  *
  95.  * Side Effects:
  96.  *    The buffer may be expanded.
  97.  *
  98.  *-----------------------------------------------------------------------
  99.  */
  100. void
  101. Buf_AddByte (buf, byte)
  102.     Buffer  buf;
  103.     Byte    byte;
  104. {
  105.     register BufPtr  bp = (BufPtr) buf;
  106.  
  107.     BufExpand (bp, 1);
  108.  
  109.     *bp->inPtr = byte;
  110.     bp->inPtr += 1;
  111. }
  112.  
  113. /*-
  114.  *-----------------------------------------------------------------------
  115.  * Buf_AddBytes --
  116.  *    Add a number of bytes to the buffer.
  117.  *
  118.  * Results:
  119.  *    None.
  120.  *
  121.  * Side Effects:
  122.  *    Guess what?
  123.  *
  124.  *-----------------------------------------------------------------------
  125.  */
  126. void
  127. Buf_AddBytes (buf, numBytes, bytesPtr)
  128.     Buffer  buf;
  129.     int        numBytes;
  130.     Byte    *bytesPtr;
  131. {
  132.     register BufPtr  bp = (BufPtr) buf;
  133.  
  134.     BufExpand (bp, numBytes);
  135.  
  136.     bcopy (bytesPtr, bp->inPtr, numBytes);
  137.     bp->inPtr += numBytes;
  138. }
  139.  
  140. /*-
  141.  *-----------------------------------------------------------------------
  142.  * Buf_UngetByte --
  143.  *    Place the byte back at the beginning of the buffer.
  144.  *
  145.  * Results:
  146.  *    SUCCESS if the byte was added ok. FAILURE if not.
  147.  *
  148.  * Side Effects:
  149.  *    The byte is stuffed in the buffer and outPtr is decremented.
  150.  *
  151.  *-----------------------------------------------------------------------
  152.  */
  153. void
  154. Buf_UngetByte (buf, byte)
  155.     Buffer  buf;
  156.     Byte    byte;
  157. {
  158.     register BufPtr    bp = (BufPtr) buf;
  159.  
  160.     if (bp->outPtr != bp->buffer) {
  161.     bp->outPtr -= 1;
  162.     *bp->outPtr = byte;
  163.     } else if (bp->outPtr == bp->inPtr) {
  164.     *bp->inPtr = byte;
  165.     bp->inPtr += 1;
  166.     } else {
  167.     /*
  168.      * Yech. have to expand the buffer to stuff this thing in.
  169.      * We use a different expansion constant because people don't
  170.      * usually push back many bytes when they're doing it a byte at
  171.      * a time...
  172.      */
  173.     int       numBytes = bp->inPtr - bp->outPtr;
  174.     Byte      *newBuf;
  175.  
  176.     newBuf = (Byte *) malloc ((unsigned) (bp->size + BUF_UNGET_INC));
  177.     bcopy ((char *) bp->outPtr, (char *) (newBuf+BUF_UNGET_INC), numBytes);
  178.     bp->outPtr = newBuf + BUF_UNGET_INC;
  179.     bp->inPtr = bp->outPtr + numBytes;
  180.     free ((char *)bp->buffer);
  181.     bp->buffer = newBuf;
  182.     bp->size += BUF_UNGET_INC;
  183.     bp->outPtr -= 1;
  184.     *bp->outPtr = byte;
  185.     }
  186. }
  187.  
  188. /*-
  189.  *-----------------------------------------------------------------------
  190.  * Buf_UngetBytes --
  191.  *    Push back a series of bytes at the beginning of the buffer.
  192.  *
  193.  * Results:
  194.  *    None.
  195.  *
  196.  * Side Effects:
  197.  *    outPtr is decremented and the bytes copied into the buffer.
  198.  *
  199.  *-----------------------------------------------------------------------
  200.  */
  201. void
  202. Buf_UngetBytes (buf, numBytes, bytesPtr)
  203.     Buffer  buf;
  204.     int        numBytes;
  205.     Byte    *bytesPtr;
  206. {
  207.     register BufPtr    bp = (BufPtr) buf;
  208.  
  209.     if (bp->outPtr - bp->buffer >= numBytes) {
  210.     bp->outPtr -= numBytes;
  211.     bcopy ((char *) bytesPtr, (char *) bp->outPtr, numBytes);
  212.     } else if (bp->outPtr == bp->inPtr) {
  213.     Buf_AddBytes (buf, numBytes, bytesPtr);
  214.     } else {
  215.     int       curNumBytes = bp->inPtr - bp->outPtr;
  216.     Byte      *newBuf;
  217.     int       newBytes = max(numBytes,BUF_UNGET_INC);
  218.  
  219.     newBuf = (Byte *) malloc ((unsigned) (bp->size + newBytes));
  220.     bcopy((char *) bp->outPtr, (char *) (newBuf+newBytes), curNumBytes);
  221.     bp->outPtr = newBuf + newBytes;
  222.     bp->inPtr = bp->outPtr + curNumBytes;
  223.     free ((char *)bp->buffer);
  224.     bp->buffer = newBuf;
  225.     bp->size += newBytes;
  226.     bp->outPtr -= numBytes;
  227.     bcopy((char *) bytesPtr, (char *) bp->outPtr, numBytes);
  228.     }
  229. }
  230.  
  231. /*-
  232.  *-----------------------------------------------------------------------
  233.  * Buf_GetByte --
  234.  *    Return the next byte from the buffer. Actually returns an integer.
  235.  *
  236.  * Results:
  237.  *    Returns BUF_ERROR if there's no byte in the buffer, or the byte
  238.  *    itself if there is one.
  239.  *
  240.  * Side Effects:
  241.  *    outPtr is incremented and both outPtr and inPtr will be reset if
  242.  *    the buffer is emptied.
  243.  *
  244.  *-----------------------------------------------------------------------
  245.  */
  246. int
  247. Buf_GetByte (buf)
  248.     Buffer  buf;
  249. {
  250.     BufPtr  bp = (BufPtr) buf;
  251.     int        res;
  252.  
  253.     if (bp->inPtr == bp->outPtr) {
  254.     return (BUF_ERROR);
  255.     } else {
  256.     res = (int) *bp->outPtr;
  257.     bp->outPtr += 1;
  258.     if (bp->outPtr == bp->inPtr) {
  259.         bp->outPtr = bp->inPtr = bp->buffer;
  260.     }
  261.     return (res);
  262.     }
  263. }
  264.  
  265. /*-
  266.  *-----------------------------------------------------------------------
  267.  * Buf_GetBytes --
  268.  *    Extract a number of bytes from the buffer.
  269.  *
  270.  * Results:
  271.  *    The number of bytes gotten.
  272.  *
  273.  * Side Effects:
  274.  *    The passed array is overwritten.
  275.  *
  276.  *-----------------------------------------------------------------------
  277.  */
  278. int
  279. Buf_GetBytes (buf, numBytes, bytesPtr)
  280.     Buffer  buf;
  281.     int        numBytes;
  282.     Byte    *bytesPtr;
  283. {
  284.     BufPtr  bp = (BufPtr) buf;
  285.     
  286.     if (bp->inPtr - bp->outPtr < numBytes) {
  287.     numBytes = bp->inPtr - bp->outPtr;
  288.     }
  289.     bcopy ((char *) bp->outPtr, (char *) bytesPtr, numBytes);
  290.     bp->outPtr += numBytes;
  291.  
  292.     if (bp->outPtr == bp->inPtr) {
  293.     bp->outPtr = bp->inPtr = bp->buffer;
  294.     }
  295.     return (numBytes);
  296. }
  297.  
  298. /*-
  299.  *-----------------------------------------------------------------------
  300.  * Buf_GetAll --
  301.  *    Get all the available data at once.
  302.  *
  303.  * Results:
  304.  *    A pointer to the data and the number of bytes available.
  305.  *
  306.  * Side Effects:
  307.  *    None.
  308.  *
  309.  *-----------------------------------------------------------------------
  310.  */
  311. Byte *
  312. Buf_GetAll (buf, numBytesPtr)
  313.     Buffer  buf;
  314.     int        *numBytesPtr;
  315. {
  316.     BufPtr  bp = (BufPtr)buf;
  317.  
  318.     if (numBytesPtr != (int *)NULL) {
  319.     *numBytesPtr = bp->inPtr - bp->outPtr;
  320.     }
  321.     
  322.     return (bp->outPtr);
  323. }
  324.  
  325. /*-
  326.  *-----------------------------------------------------------------------
  327.  * Buf_Discard --
  328.  *    Throw away bytes in a buffer.
  329.  *
  330.  * Results:
  331.  *    None.
  332.  *
  333.  * Side Effects:
  334.  *    The bytes are discarded. 
  335.  *
  336.  *-----------------------------------------------------------------------
  337.  */
  338. void
  339. Buf_Discard (buf, numBytes)
  340.     Buffer  buf;
  341.     int        numBytes;
  342. {
  343.     register BufPtr    bp = (BufPtr) buf;
  344.  
  345.     if (bp->inPtr - bp->outPtr <= numBytes) {
  346.     bp->inPtr = bp->outPtr = bp->buffer;
  347.     } else {
  348.     bp->outPtr += numBytes;
  349.     }
  350. }
  351.  
  352. /*-
  353.  *-----------------------------------------------------------------------
  354.  * Buf_Size --
  355.  *    Returns the number of bytes in the given buffer.
  356.  *
  357.  * Results:
  358.  *    The number of bytes.
  359.  *
  360.  * Side Effects:
  361.  *    None.
  362.  *
  363.  *-----------------------------------------------------------------------
  364.  */
  365. int
  366. Buf_Size (buf)
  367.     Buffer  buf;
  368. {
  369.     return (((BufPtr)buf)->inPtr - ((BufPtr)buf)->outPtr);
  370. }
  371.  
  372. /*-
  373.  *-----------------------------------------------------------------------
  374.  * Buf_Init --
  375.  *    Initialize a buffer. If no initial size is given, a reasonable
  376.  *    default is used.
  377.  *
  378.  * Results:
  379.  *    A buffer to be given to other functions in this library.
  380.  *
  381.  * Side Effects:
  382.  *    The buffer is created, the space allocated and pointers
  383.  *    initialized.
  384.  *
  385.  *-----------------------------------------------------------------------
  386.  */
  387. Buffer
  388. Buf_Init (size)
  389.     int        size;     /* Initial size for the buffer */
  390. {
  391.     BufPtr  bp;          /* New Buffer */
  392.  
  393.     bp = (Buf *) malloc(sizeof(Buf));
  394.  
  395.     if (size <= 0) {
  396.     size = BUF_DEF_SIZE;
  397.     }
  398.     bp->size = size;
  399.     bp->buffer = (Byte *) malloc ((unsigned) size);
  400.     bp->inPtr = bp->outPtr = bp->buffer;
  401.  
  402.     return ((Buffer) bp);
  403. }
  404.  
  405. /*-
  406.  *-----------------------------------------------------------------------
  407.  * Buf_Destroy --
  408.  *    Nuke a buffer and all its resources.
  409.  *
  410.  * Results:
  411.  *    None.
  412.  *
  413.  * Side Effects:
  414.  *    The buffer is freed.
  415.  *
  416.  *-----------------------------------------------------------------------
  417.  */
  418. void
  419. Buf_Destroy (buf, freeData)
  420.     Buffer  buf;      /* Buffer to destroy */
  421.     Boolean freeData;    /* TRUE if the data should be destroyed as well */
  422. {
  423.     BufPtr  bp = (BufPtr) buf;
  424.     
  425.     if (freeData) {
  426.     free ((char *)bp->buffer);
  427.     }
  428.     free ((char *)bp);
  429. }
  430. @
  431.  
  432.  
  433. 1.1
  434. log
  435. @Initial revision
  436. @
  437. text
  438. @d19 1
  439. a19 1
  440. "$Header: buf.c,v 2.0 87/08/11 09:33:11 brent Exp $ SPRITE (Berkeley)";
  441. d47 1
  442. a47 1
  443.         bcopy ((bp)->outPtr, newBuf, (bp)->inPtr - (bp)->outPtr);
  444. @
  445.